package com.hxzy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.consts.UserConstants;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.HospitalException;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.vo.R;
import com.hxzy.service.SysMenuService;
import com.hxzy.service.SysRoleService;
import com.hxzy.service.SysUserService;
import com.hxzy.mapper.SysUserMapper;
import com.hxzy.system.dto.AdminLoginDTO;
import com.hxzy.system.dto.SysUserDTO;
import com.hxzy.system.dto.SysUserSearchDTO;
import com.hxzy.system.entity.SysUser;
import com.hxzy.system.feign.SystemServiceFeignClient;
import com.hxzy.system.vo.SysUserVO;
import com.hxzy.system.vo.AdminLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author Administrator
* @description 针对表【sys_user(医生信息表)】的数据库操作Service实现
* @createDate 2023-03-10 11:56:11
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 后台用户登录 （spring security的登录流程规范）
     * @param adminLoginDTO
     * @return
     */
    @Override
    public AdminLoginVO login(AdminLoginDTO adminLoginDTO) {

        SysUserVO  dbUser = this.getBaseMapper().findByAccount(adminLoginDTO.getAccount());
        if (dbUser == null) {
            log.error(adminLoginDTO.getAccount() + "," + AckCode.USER_NOT_FOUND.getMsg());
            throw new ServiceException(AckCode.LOGIN_ACCOUNT_PASSWORD_ERROR);
        }

        // 状态
        String status = dbUser.getStatus();
        if (status == UserConstants.USER_DISABLE) {
            log.error(adminLoginDTO.getAccount() + "," + AckCode.DEVICE_BANNED.getMsg());
            throw new ServiceException(AckCode.DEVICE_BANNED);
        }

        //自定义当前用户对象
        //自定义数据
        AdminLoginVO adminLoginVO=new AdminLoginVO();
        adminLoginVO.setSysUser(dbUser);

        //得到角色信息
        Set<String> roleSet= new HashSet<String>();
        if(dbUser.getRole()!=null){
            roleSet.add(dbUser.getRole().getRoleKey());
        }
        adminLoginVO.setRoleSet(roleSet);

        //查询菜单角色
        Set<String> permSet=new HashSet<>();
        //判断是不是超级管理员
        if(adminLoginVO.isSuperMan()){
            permSet.add("*:*:*");
        }else{
            permSet= this.sysMenuService.searchUserOwnPerm(dbUser.getUserId());
        }
        adminLoginVO.setPermsSet(permSet);
        //返回spring security规范的对象 UserDetails
        return adminLoginVO;
    }


    /**
     * 分页查询
     * @param sysUserDTO
     * @return
     */
    @Override
    public List<SysUserVO> search(SysUserDTO sysUserDTO) {
        return this.baseMapper.search( sysUserDTO);
    }

    @Override
    public int removeByIds(Long[] ids) {
        int count = 0;
        if (ids != null && ids.length > 0) {
            List<Long> arr = Arrays.asList(ids);
            if (arr.contains(1L)) {
                arr.remove(1L);
            }
            //批量更新
            count = super.getBaseMapper().removeByIds(ids);
        }
        return count;
    }

    @Override
    public int updateStatus(SysUserSearchDTO dto) {
        return this.getBaseMapper().updateStatus(dto.getId(), dto.getStatus());
    }

    /**
     * 根据用户名查询用户信息
     * @param username
     * @return
     */
    @Override
    public SysUserVO findUserByName(String username) {
        return this.getBaseMapper().findByAccount(username);
    }



}




